//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.cnxfs.toolbox.controller;

import cn.cnxfs.toolbox.service.IToolService;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"/"})
public class ToolController {
    private static final Logger logger = LoggerFactory.getLogger(ToolController.class);
    @Autowired
    private IToolService iToolService;

    public ToolController() {
    }

    @RequestMapping({"/hello"})
    @ResponseBody
    public String hello() {
        logger.info("进入/hello");
        return "JSP服务正常";
    }

    @RequestMapping(
            value = {"/url"},
            method = {RequestMethod.GET}
    )
    public void url(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect(request.getContextPath() + "/jsp/tools/urlcode.jsp");
    }

    @RequestMapping(
            value = {"url"},
            method = {RequestMethod.POST}
    )
    public ModelAndView url(@RequestParam(required = false,defaultValue = "") String str, @RequestParam(required = false,defaultValue = "decode") String type) {
        logger.info("请求/url");
        String result;
        if (!StringUtils.hasText(str)) {
            logger.warn("未输入URL！！");
            result = "请输入URL！！";
        } else if (type.equals("decode")) {
            logger.info("准备将{}进行解码", str);
            result = this.iToolService.urlDecode(str);
        } else {
            logger.info("准备将{}进行编码", str);
            result = this.iToolService.urlEncode(str);
        }

        logger.info("结果为{}", result);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("tools/urlcode");
        modelAndView.addObject("str", str);
        modelAndView.addObject("type", type);
        modelAndView.addObject("result", result);
        return modelAndView;
    }

    @RequestMapping(
            value = {"/md5"},
            method = {RequestMethod.GET}
    )
    public void md5(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect(request.getContextPath() + "/jsp/tools/md5.jsp");
    }

    @RequestMapping(
            value = {"/md5"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public ModelAndView md5(@RequestParam(required = false,defaultValue = "") String str, @RequestParam(required = false,defaultValue = "small") String type) {
        logger.info("请求/md5");
        String md5;
        if (!StringUtils.hasText(str)) {
            logger.warn("未输入MD5原文！");
            md5 = "请输入原文！";
        } else if (type.equals("big")) {
            logger.info("准备将{}转为大写的MD5值", str);
            md5 = this.iToolService.getMD5(str, true);
        } else {
            logger.info("准备将{}转为小写的MD5值", str);
            md5 = this.iToolService.getMD5(str, false);
        }

        logger.info("########结果为{}", md5);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/tools/md5");
        md5 = "####" + md5;
        modelAndView.addObject("md5", md5);
        modelAndView.addObject("str", str);
        modelAndView.addObject("type", type);
        return modelAndView;
    }

    @RequestMapping({"/IPv6"})
    public ModelAndView IPv6(HttpServletRequest request) {
        logger.info("请求/IPv6");
        String ip = this.iToolService.getRealIp(request);
        logger.info("IP地址为{}", ip);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/tools/IPv6");
        modelAndView.addObject("type", ip.contains(".") ? "IPv4" : "IPv6");
        modelAndView.addObject("address", ip);
        return modelAndView;
    }
}
